/*
 * CAppStateManager.cpp
 *
 *  Created on: 24/12/2012
 *
 */

#include "CAppStateManager.h"
#include "CAppStateIntro.h"
#include "CAppStateMenu.h"
#include "CAppStateCreateGame.h"
#include "CAppStatePlayGame.h"
#include "CAppStateGame.h"
#include "CAppStateStage.h"
#include "DataLoader.h"
#include "CAppStateEnd.h"
#include "CAppStateLostConnection.h"
#include <iostream>

using namespace std;

CAppState* CAppStateManager::activeAppState = 0;

void CAppStateManager::onEvent(SDL_Event* eventHolder) {
    if(activeAppState) activeAppState->onEvent(eventHolder);
}

void CAppStateManager::onLoop() {
    if(activeAppState) activeAppState->onLoop();
}

void CAppStateManager::onRender(SDL_Surface* surf_Display) {
    if(activeAppState) activeAppState->onRender(surf_Display);
}

void CAppStateManager::setActiveAppState(int appStateID, DataLoader* user) {
    if(activeAppState) activeAppState->onDeactivate();

    switch(appStateID){
    	case APPSTATE_NONE:
    		activeAppState = 0;
    		break;
    	case APPSTATE_INTRO:
    		activeAppState = CAppStateIntro::getInstance();
    		break;
    	case APPSTATE_MENU:
    		 activeAppState = CAppStateMenu::getInstance();
    		 break;
    	case APPSTATE_CREATE:
    		 activeAppState = CAppStateCreateGame::getInstance();
    		 break;
    	case APPSTATE_PLAY:
    		activeAppState = CAppStatePlayGame::getInstance();
    		break;
    	case APPSTATE_LOST_CONNECTION:
    		activeAppState = CAppStateLostConnection::getInstance();
    		break;
    	case APPSTATE_GAME:
    		activeAppState = CAppStateGame::getInstance();
    		CAppStateGame* stateGameClient;
    		stateGameClient = (CAppStateGame*) activeAppState;
    		stateGameClient->setUser(user);
    		break;
    	case APPSTATE_STAGE:
    		CAppStateStage* stage_client;
    		stage_client = CAppStateStage::getInstance();
    		stage_client->setUser(*user);
    		activeAppState = stage_client;
    		break;
    	case APPSTATE_END:
    		CAppStateEnd* end;
    		end = CAppStateEnd::getInstance();
    		end->setData(user);
    		activeAppState = end;
    		break;
    	default:
    		break;

    }

    if(activeAppState) activeAppState->onActivate();
}

CAppState* CAppStateManager::getActiveAppState() {
    return activeAppState;
}


